<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="老人编号" prop="oldId">
      <el-input v-model="dataForm.oldId" placeholder="老人编号"></el-input>
    </el-form-item>
    <el-form-item label="老人名称" prop="oldName">
      <el-input v-model="dataForm.oldName" placeholder="老人名称"></el-input>
    </el-form-item>
    <el-form-item label="房间号" prop="roomId">
      <el-input v-model="dataForm.roomId" placeholder="房间号"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startDate">
      <el-date-picker
      v-model="dataForm.startDate"
      type="datetime"
      placeholder="选择日期"
      format="yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒"
      value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" prop="finDate">
      <el-date-picker
      v-model="dataForm.finDate"
      type="datetime"
      placeholder="选择日期"
      format="yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒"
      value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="陪同人" prop="chaperone">
      <el-input v-model="dataForm.chaperone" placeholder="陪同人"></el-input>
    </el-form-item>
    <el-form-item label="操作时间" prop="operaDate">
      <el-date-picker
      v-model="dataForm.operaDate"
      type="datetime"
      placeholder="选择日期"
      format="yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒"
      value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="批准人" prop="approvingOfficer">
      <el-input v-model="dataForm.approvingOfficer" placeholder="批准人"></el-input>
    </el-form-item>
    <el-form-item label="原因" prop="reason">
      <el-input v-model="dataForm.reason" placeholder="原因"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          oldId: '',
          oldName: '',
          roomId: '',
          finDate: '',
          startDate: '',
          chaperone: '',
          operaDate: '',
          approvingOfficer: '',
          reason: ''
        },
        dataRule: {
          oldId: [
            { required: true, message: '老人编号不能为空', trigger: 'blur' }
          ],
          oldName: [
            { required: true, message: '老人名称不能为空', trigger: 'blur' }
          ],
          roomId: [
            { required: true, message: '房间号不能为空', trigger: 'blur' }
          ],
          finDate: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '开始日期不能为空', trigger: 'blur' }
          ],
          chaperone: [
            { required: true, message: '陪同人不能为空', trigger: 'blur' }
          ],
          operaDate: [
            { required: true, message: '操作时间不能为空', trigger: 'blur' }
          ],
          approvingOfficer: [
            { required: true, message: '批准人不能为空', trigger: 'blur' }
          ],
          reason: [
            { required: true, message: '原因不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/wgzgoingoutregistration/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.oldId = data.wgzGoingoutRegistration.oldId
                this.dataForm.oldName = data.wgzGoingoutRegistration.oldName
                this.dataForm.roomId = data.wgzGoingoutRegistration.roomId
                this.dataForm.finDate = data.wgzGoingoutRegistration.finDate
                this.dataForm.startDate = data.wgzGoingoutRegistration.startDate
                this.dataForm.chaperone = data.wgzGoingoutRegistration.chaperone
                this.dataForm.operaDate = data.wgzGoingoutRegistration.operaDate
                this.dataForm.approvingOfficer = data.wgzGoingoutRegistration.approvingOfficer
                this.dataForm.reason = data.wgzGoingoutRegistration.reason
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/wgzgoingoutregistration/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'oldId': this.dataForm.oldId,
                'oldName': this.dataForm.oldName,
                'roomId': this.dataForm.roomId,
                'finDate': this.dataForm.finDate,
                'startDate': this.dataForm.startDate,
                'chaperone': this.dataForm.chaperone,
                'operaDate': this.dataForm.operaDate,
                'approvingOfficer': this.dataForm.approvingOfficer,
                'reason': this.dataForm.reason
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
